package com.sg.plugin.finance.config;


import com.alibaba.fastjson.JSONObject;
import com.wicket.okrfinance.common.exception.BizException;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class FeignErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String methodKey, Response response) {
        String msg = null;
        JSONObject jo = null;
        try {
            String resultStr = Util.toString(response.body().asReader(Util.UTF_8));
            jo = JSONObject.parseObject(resultStr);

        } catch (IOException e) {
            e.printStackTrace();
        }
        // 抛出自定义的业务异常
        throw new BizException(jo.getString("code"), jo.getString("message"), false);

    }
}